package com.eleventwelve.iris.web.validator;


import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.multipart.MultipartFile;

import com.eleventwelve.iris.entity.Patient;
import com.eleventwelve.iris.utilities.ScannedFileContainer;

public class ScannedFileValidator implements Validator{

	@Override
	public boolean supports(Class<?> arg0) {
		return Patient.class.isAssignableFrom(arg0);
	}

	@Override
	public void validate(Object obj, Errors err) {
		
		ScannedFileContainer container = (ScannedFileContainer) obj;
		
		MultipartFile multipartFile = container.getTempFile();
		
		if (multipartFile != null && !multipartFile.isEmpty()) {
			
			String contentType = multipartFile.getContentType().substring(0, 6);

			if (!"image/".equals(contentType)) {

				err.rejectValue("photo",
						"error.scanned-file.invalid-file-format",
						"Invalid file. Profile Image must be in PNG, JPEG, GIF or BMP format.");

			} 
		}
	}

}
